package com.huynv.redblue;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.graphics.Color;
/**
 * LovelyView demonstrates a custom view
 * for Mobiletuts+ tutorial - Android SDK: Creating Custom Views
 * 
 * The view displays a circle with a text string displayed in the middle.
 * Circle color, text and text color can all be set in layout XML or Java
 * - see the main app Activity
 * 
 * The view also refers to attributes specified in the app res/values/attrs XML
 */

public class gameView extends View {

	//circle and text colors
	private int circleCol, labelCol;
	//label text
	private String circleText;
	//paint for drawing custom view
	private Paint circlePaint;
	private int totalPoints=0;
	private int currentColor=0;//0=true, 1=false;
	private boolean isGameOver=false;
	private long oldTime=0;
	private boolean isReadyGame=true;
	private int countReadyGame=3;
	private int viewWidthHalf = this.getMeasuredWidth();
	private int viewHeightHalf = this.getMeasuredHeight();
	
	private Paint[] mForegrounds =
		{ 
				makePaint(Color.BLUE), makePaint(Color.RED)
		};	
	private Paint makePaint(int color) {
		Paint p = new Paint();
		p.setColor(color);
		return(p);
	}
	
	/**
	 * Constructor method for custom view
	 * - calls superclass method and adds custom processing
	 * 
	 * @param context
	 * @param attrs
	 */
	public gameView(Context context, AttributeSet attrs){
		super(context, attrs);
		Log.e("LovelyView", "ok1");
		//paint object for drawing in onDraw
		circlePaint = new Paint();
		totalPoints=0;
		//get the attributes specified in attrs.xml using the name we included
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.gameView, 0, 0);
		isReadyGame=true;
		countReadyGame=3;
		oldTime=0;
		try {
			//get the text and colors specified using the names in attrs.xml
			circleText = a.getString(R.styleable.gameView_circleLabel);
			circleCol = a.getInteger(R.styleable.gameView_circleColor, 0);//0 is default
			labelCol = a.getInteger(R.styleable.gameView_labelColor, 0);
		} finally {
			a.recycle();
		}
	}	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
	    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
	    int newHeight=Math.round(this.getMeasuredHeight()*85/100);
	    this.setMeasuredDimension(parentWidth, newHeight);
	}
	/**
	 * Override the onDraw method to specify custom view appearance using canvas
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		viewWidthHalf = this.getMeasuredWidth();
		viewHeightHalf = this.getMeasuredHeight();
		//if(isReadyGame)//ready for new game
		//{
			//circlePaint.setColor(labelCol);
			//set text properties
			//circlePaint.setTextAlign(Paint.Align.CENTER);
			//circlePaint.setColor(Color.RED);
			//circlePaint.setTextSize(50);
			//canvas.drawText(String.valueOf(countReadyGame),viewWidthHalf/2, viewHeightHalf/2, circlePaint);
			//return;
		//}
		totalPoints++;
		//get half of the width and height as we are working with a circle
		//Log.e("onDraw", "ok1");
		//int viewWidthHalf = this.getMeasuredWidth();
		//int viewHeightHalf = this.getMeasuredHeight();
		//get the radius as half of the width or height, whichever is smaller
		//subtract ten so that it has some space around it
		int radius = 0;
		if(viewWidthHalf>viewHeightHalf)
			radius=viewHeightHalf/4;
		else
			radius=viewWidthHalf/4;
		
		//set drawing properties
		circlePaint.setStyle(Style.FILL);
		circlePaint.setAntiAlias(true);
		//set the paint color using the circle color specified
		//circlePaint.setColor(circleCol);
		//draw the circle using the properties defined		
		//canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);
		
		//random
		float x = RandomUtils.randomFloat(viewWidthHalf-2*radius)+radius;
		float y = RandomUtils.randomFloat(viewHeightHalf-2*radius)+radius;
		//x=viewWidthHalf/2;
		//y=viewHeightHalf/2;
		Paint circleColor = RandomUtils.randomElement(mForegrounds);
		
		if (totalPoints<2) {currentColor=0;}
		else {currentColor=RandomUtils.randomInt(2);}//for ready	
		if (!isGameOver){
			if (currentColor==0)
			{
				if (circleColor.getColor()==Color.BLUE){
					circleText="BLUE?";
				}else{
					circleText="RED?";
				}
			}else{
				if (circleColor.getColor()==Color.BLUE){
					circleText="RED?";
				}else{
					circleText="BLUE?";
				}
			}
		}
		oldTime=System.currentTimeMillis();
		
		//Log.e("draw circle", String.valueOf(totalPoints));
		//set the text color using the color specified
		circlePaint.setColor(circleColor.getColor());
		//set text properties
		circlePaint.setTextAlign(Paint.Align.CENTER);
		int textSize=RandomUtils.randomInt(40)+100;
		circlePaint.setTextSize(textSize);
		//draw the text using the string attribute and chosen properties
		if(isGameOver){
			//circleText="Game over!";
			x=viewWidthHalf/2;
			y=viewHeightHalf/2;
			circlePaint.setTextSize(30);
		}
		canvas.drawText(circleText,x,y,circlePaint);
	}

	//each custom attribute should have a get and set method
	//this allows updating these properties in Java
	//we call these in the main Activity class

	/**
	 * Get the current circle color
	 * @return color as an int
	 */
	public int getCircleColor(){
		return circleCol;
	}
	public int getCurrentColor(){
		return currentColor;		
	}
	/**
	 * Set the circle color
	 * @param newColor new color as an int
	 */
	public void setCircleColor(int newColor){
		//update the instance variable
		circleCol=newColor;
		//redraw the view
		invalidate();
		requestLayout();
	}

	/**
	 * Get the current text label color
	 * @return color as an int
	 */
	public int getLabelColor(){
		return labelCol;
	}
	
	/**
	 * Set the label color
	 * @param newColor new color as an int
	 */
	public void setLabelColor(int newColor){
		//update the instance variable
		labelCol=newColor;
		//redraw the view
		invalidate();
		requestLayout();
	}

	/**
	 * Get the current label text
	 * @return text as a string
	 */
	public String getLabelText(){
		return circleText;
	}
	public int getCountReadyGame(){
		return countReadyGame;
	}
	public void setCountReadyGame(int v){
		countReadyGame=v;
		//invalidate();
		//requestLayout();
	}
	public void setIsReadyGame(boolean v){
		isReadyGame=v;
	}
	public long getOldTime(){
		return oldTime;
	}
	/**
	 * Set the label text
	 * @param newLabel text as a string
	 */
	public void setLabelText(String newLabel){
		//update the instance variable
		circleText=newLabel;
		//redraw the view
		invalidate();
		requestLayout();
	}
	public boolean getIsGameOver(){
		return isGameOver;
	}
	public void setGameOver(String newLabel){
		circleText=newLabel;
		isGameOver=true;
		totalPoints=0;
		oldTime=0;
	}
	public void setRestartGame(){
		isGameOver=false;
		totalPoints=0;
		oldTime=0;
	}
	
}
